package com.overthewall.component;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.util.ArrayList;
import java.util.Random;

import javax.swing.JComponent;

import com.overthewall.object.LineObject;
import com.overthewall.view.InputViewer;
import com.overthewall.view.MainViewer;

public class LineComponent extends JComponent {

	private static final int COLUMN_GAP = 70;
	private static final int TOP_X = 50;
	private static final int TOP_Y = 50;
	
	private static final int LINE_LENGTH = (int) (MainViewer.frameHeight*(0.5));
	
	private static final int SIZE = 40;
	
	private ArrayList<Point> pointList;
	private ArrayList<Point> footHoldPoint;
	
	// Random Number Generator
	private Random rand = new Random();
	
	public void paintComponent(Graphics g)
	{
		Graphics2D g2 = (Graphics2D)g;
		
		pointList = getPosition();
		footHoldPoint = getFootHoldPosition(pointList);
		
		LineObject line;
		
		for (Point element : pointList) {
			line = new LineObject(element, new Point(element.x, element.y + LINE_LENGTH));
			line.draw(g2);
		}
		
		for (Point element : footHoldPoint) {
			line = new LineObject(element, new Point(element.x + COLUMN_GAP, element.y));
			line.draw(g2);
		}
		
	}
	
	private ArrayList<Point> getPosition()
	{
		ArrayList<Point> pointList = new ArrayList<>();
		
		int columnNum = MainViewer.frameWidth/COLUMN_GAP;
		
		Point position;
		
		for (int i = 0; i < InputViewer.inputColumnNum; i++) {
			position = new Point(TOP_X+(i*COLUMN_GAP), TOP_Y);
			pointList.add(position);
		}
		
		return pointList;
	}
	
	private ArrayList<Point> getFootHoldPosition(ArrayList<Point> columnList)
	{
		ArrayList<Point> pointList = new ArrayList<>();
		
		Point position;
		
		for (int i = 0; i < SIZE; i++) {
			
			position = new Point(columnList.get(rand.nextInt(columnList.size()-1)).x , rand.nextInt(LINE_LENGTH)+TOP_Y);
			pointList.add(position);
			
		}
		
		return pointList;
	}
}
